package com.SFA.DownLoadPhoto;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.FrameWork.Common.AbstractBaseActivity;
import com.FrameWork.Common.CommonFunction;
import com.SFA.R;
import com.SFA.R.string;

public class ProductPhotoDownloadActivity extends AbstractBaseActivity implements OnClickListener{

	private ImageView dp_img;
	private Button btn_down_photo;
	private ProgressBar download_pb;
	private TextView tx_down_photo;
	private List<ProductPhotoEntity> ppeList = null;
	private List<ProductPhotoEntity> notExistPpeList = null;
	private int unDownloadNum = 0;
	private int DownloadedNum = 0;
	private boolean bool_download = false;
	private boolean IsPause = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo_download);
		dp_img = (ImageView)findViewById(R.id.dp_img);
		btn_down_photo = (Button)findViewById(R.id.btn_down_photo);
		tx_down_photo = (TextView)findViewById(R.id.tx_down_photo);
		download_pb = (ProgressBar)findViewById(R.id.download_pb);
		btn_down_photo.setOnClickListener(this);

		dialog = CommonFunction.ShowProgressDialog(context, R.string.MSG_PHOTO_001, false);
		dialog.show();
		new Thread(run).start();
	}

	
	private Runnable run = new Runnable()
	{

		@Override
		public void run() {
			ppeList = DownloadPhotoUtils.allPhotoInfo(context);
			notExistPpeList = DownloadPhotoUtils.notExistPhotoInfo(ppeList);
			dlHandler.sendEmptyMessage(0);
		}

	};

	@Override
	public void onClick(View v) {
		// 下载不存在的图片
		if(!bool_download){
			bool_download = true;
			IsPause = false;
			this.btn_down_photo.setText("开始启动下载图片....");
			new Thread(dlRunnable).start();

		}
		else
		{
			IsPause= true;
			dlHandler.sendEmptyMessage(5);
		}
	}


	private Handler dlHandler = new Handler(){

		@Override
		public synchronized void handleMessage(Message msg) {
			super.handleMessage(msg);   
			if(dialog != null)
			{
				dialog.dismiss();
				dialog = null;
			}
			switch(msg.what){
			case 0:
				DownloadedNum =ppeList.size();
				unDownloadNum = notExistPpeList.size();
				DownloadedNum = DownloadedNum-unDownloadNum;
				if(unDownloadNum==0)
					btn_down_photo.setText(R.string.MSG_PHOTO_002);
				else
					btn_down_photo.setText(String.format(context.getResources().getString(R.string.MSG_PHOTO_004), unDownloadNum));
				download_pb.setMax(unDownloadNum+DownloadedNum );
				download_pb.setProgress(DownloadedNum);
				tx_down_photo.setText(String.format(context.getResources().getString(R.string.MSG_PHOTO_005), DownloadedNum,unDownloadNum));
				break;
			case 3:
				unDownloadNum -= 1;
				DownloadedNum += 1;
				download_pb.setProgress(DownloadedNum);
				tx_down_photo.setText(String.format(context.getResources().getString(R.string.MSG_PHOTO_005), DownloadedNum,unDownloadNum));
				btn_down_photo.setText(R.string.MSG_PHOTO_006);
				Bundle b = msg.getData();
				String url = b.getString("saveUrl");
				Bitmap bm = BitmapFactory.decodeFile(url);
				dp_img.setImageBitmap(bm);
				System.gc();
				break;
			case 5:
				btn_down_photo.setText(R.string.MSG_PHOTO_003);
				break;
			}
		}

	};

	private Runnable dlRunnable = new Runnable(){

		@Override
		public void run() {

			for(ProductPhotoEntity entity :notExistPpeList)
			{
				if(IsPause)
				{
					break;
				}
				if(entity != null)
				{
					try
					{
						String saveUrl = DownloadPhotoUtils.downloadBitmap(context,entity.getPhotoName(), entity.getPhotoName());
						Message msg3 = new Message();
						msg3.what = 3;                 
						Bundle b = new Bundle();// 存放数据
						b.putString("saveUrl", saveUrl);
						msg3.setData(b);
						dlHandler.sendMessage(msg3);
					}
					catch(Exception err)
					{
						
					}
				}
			}
			new Thread(run).start();
			bool_download = false;
		}

	};

}
